package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author kk
 */
@Slf4j
@Service
@Transactional
public class MaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements MaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    //上传图片
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //校验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmUser user = WmThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //注入minio的对象进行文件上传
        String fileName = UUID.randomUUID().toString();
        String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        String path = fileStorageService.uploadImgFile("", fileName+suffix, multipartFile.getInputStream());

        //保存WmMaterial对象到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setUserId(user.getId());
        wmMaterial.setUrl(path);
        wmMaterial.setCreatedTime(new Date());

        save(wmMaterial);
        //返回对象
        return ResponseResult.okResult(wmMaterial);
    }
    //素材列表查询
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser user = WmThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置分页参数
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        //设置查询条件,收藏可查可不查
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getIsCollection() != null){//是否收藏
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        queryWrapper.eq(WmMaterial::getUserId,user.getId());
        //查询
        page(page,queryWrapper);
        //封装参数
        PageResponseResult result = new PageResponseResult();//分页类
        result.setData(page.getRecords());
        result.setSize(dto.getSize());
        result.setCurrentPage(dto.getPage());
        result.setTotal((int) page.getTotal());
        return result;
    }
}
